#include <stdio.h>
#include <math.h>

typedef struct cplx
{
  double a, b;
} cplx;

typedef struct solution
{
  cplx x1, x2;
} solution;


typedef struct coefs
{
  double a, b, c;
} coefs;

double getDouble (char * prompt)
{
  double val;
  printf ("%s", prompt);
  scanf ("%lf", &val);
  return val;
}

coefs getCoefs (int argc, char *argv[])
{
  coefs res;

  return res;
}

void printIntro ()
{
  printf ("This program solves quadratic equations in the form ax^2 + bx + c = 0\n");
}


solution solveQuadrEq (double a, double b, double c)
{
  solution sol;
  double D;

  if (a==0.0)
  {
    printf ("The equation is not quadratic, quiting.\n");
    return;
  }

  D = b*b - 4*a*c;

  if (!b) // for some reason printf thinks that zero can have a minus sign...
    sol.x1.a = sol.x2.a = 0;
  else
    sol.x1.a = sol.x2.a = (-b) / (2*a);

  if (D>0)
  {
    sol.x1.a += sqrt(D) / (2*a);
    sol.x2.a -= sqrt(D) / (2*a);
    sol.x1.b = sol.x2.b = 0;
  }
  if (D==0)
    sol.x1.b = sol.x2.b = 0;
  if (D<0)
  {
    sol.x1.b = sqrt(-D) / (2*a);
    sol.x2.b = -sqrt(-D) / (2*a);
  }

  return sol;
}


void printSolution (solution sol)
{
  if (sol.x1.a == sol.x2.a && sol.x1.b == sol.x2.b)
  {
    printf ("%lf\n", sol.x1.a);
  }
  else
  {
    if (sol.x1.b)
      printf ("%lf %+lfi\n%lf %+lfi\n", sol.x1.a, sol.x1.b, sol.x2.a, sol.x2.b);
    else
      printf ("%lf\n%lf\n", sol.x1.a, sol.x2.a);
  }
}

int main (int argc, char *argv[])
{
  double a=0,b,c;
  solution sol;
  printIntro ();
  
  while (!a)
  {
    a = getCoef ("Enter a: ");
    if (a) break;
    printf ("Not quadratic - \"a\" mustn't be zero. Try again.\n");
  }
  b = getCoef ("Enter b: ");
  c = getCoef ("Enter c: ");

  sol = solveQuadrEq (a, b, c);

  printSolution (sol);

  return 0;
}
